<?php
/**
 * Created by PhpStorm.
 * User: 刘海平（785313152）
 * Date: 2019/4/1
 * Time: 11:20
 * Message: 对未来没概念，也不想有概念，脑子里只关心汤面会不会太咸！
 */

namespace app\company\controller;



use app\admin\model\Companyuser;
use app\index\model\Jianlis;
use app\index\model\Jianlisqz;
use app\index\model\Job;
use app\index\model\Toudi;
use app\index\model\Users;
use think\Controller;
use think\Db;

class index extends Controller
{
    public function login(){
        $code = input('code');
        $state = input('state');
        if($code&&($state=='gzt')){
            $access_token = get_access_token1();
            $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$access_token.'&code='.$code;
            $res = httpGet($url);
            $res = json_decode($res,true);
            if($res['errcode']==0) {
                $get_userid_url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='
                    . $access_token . '&userid=' . $res['UserId'];
                $user_info = json_decode(httpGet($get_userid_url), true);
                if ($user_info['errcode']==0){
                    session('company_user_info',$user_info);
                    if(session('location_url')){
                        $this->redirect(session('location_url'));
                    }
                    $this->index();
                }
            }
        }else{
            $uri = urlencode(site_url. '/company/index/index'); //授权成功返回地址
            //下面$url请求授权登录地址,设置的是手动授权
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
                .get_appId().'&redirect_uri='
                .$uri.'&response_type=code&scope=snsapi_base&state=gzt#wechat_redirect';
            header('Location:' . $url);exit;
        }
    }

    public function index()
    {
        if(!session('?company_user_info')){
            session('location_url', request()->domain().request()->url());
            $this->login();
        }
        return $this->fetch();
    }
    public function jl()
    {
        $userData = session('company_user_info');
        $userId = $userData['userid'];
        $mCompanyuser = new Companyuser();
        $city = $mCompanyuser->where(['id'=>$userId])->value('city');
        if (input('type')==1){
            //兼职
            $mJianlis = new Jianlis();
            $lists = $mJianlis->where(['newaddress'=>['like',"{$city}%"],'sub_type'=>1])->field('id,name,update_time,add_time')->order('update_time desc,add_time desc')->select();
        }elseif(input('type')==2){
            //全职
            $mJianlisqz = new Jianlisqz();
            $lists = $mJianlisqz->where(['newaddress'=>['like',"{$city}%"],'sub_type'=>1])->field('id,name,update_time,add_time')->order('update_time desc,add_time desc')->select();
        }else{
            //职位投递
            $mJob = new Job();
            $mToudi = new Toudi();
            $mUsers = new Users();
            $mJianlis = new Jianlis();
            $mJianlisqz = new Jianlisqz();
            //投递按时间排序的列表
            $lists = $mToudi->order('add_time desc')->select();
            foreach ($lists as &$value) {
                //投递的职位信息
                $value['jobdata'] = $mJob->where(['jobid'=>$value['jobid'],'city'=>['like',"{$city}%"]])->field('jobid,jobname')->find();
                //当投递简历为兼职简历时
                if ($value['type']==1){
                    $value['userdata'] = $mJianlis->where(['id'=>$value['jlid']])->field('id,name')->find();
                }else{
                    //当投递简历为全职职简历时
                    $value['userdata'] = $mJianlisqz->where(['id'=>$value['jlid']])->field('id,name')->find();
                }
            }
            return $this->fetch('zw_list',['lists'=>$lists]);
        }
        $this->assign('type',input('type'));
        return $this->fetch('lists',['lists'=>$lists]);
    }

    public function edit()
    {
        $id = input('id');
        $type = input('type'); //1兼职 2全职
        if ($type==1){
            $jianlis = new Jianlis();
            $res = $jianlis->where(['id'=>$id])->find();
            $res['shixi_start1'] = json_decode($res['shixi_start1'],true);
            $res['shixi_end1'] = json_decode($res['shixi_end1'],true);
            $res['shixi_start2'] = json_decode($res['shixi_start2'],true);
            $res['shixi_end2'] = json_decode($res['shixi_end2'],true);
            $res['shixi_company'] = json_decode($res['shixi_company'],true);
            $res['shixi_zw'] = json_decode($res['shixi_zw'],true);
            $res['shixi_daiyu'] = json_decode($res['shixi_daiyu'],true);
            $this->assign('user_data',$res);
            return $this->fetch('jz');
        }else{
            $jianlisqz = new Jianlisqz();
            $res = $jianlisqz->where(['id'=>$id])->find();
            $res['shixi_start1'] = json_decode($res['shixi_start1'],true);
            $res['shixi_end1'] = json_decode($res['shixi_end1'],true);
            $res['shixi_start2'] = json_decode($res['shixi_start2'],true);
            $res['shixi_end2'] = json_decode($res['shixi_end2'],true);
            $res['shixi_company'] = json_decode($res['shixi_company'],true);
            $res['shixi_zw'] = json_decode($res['shixi_zw'],true);
            $res['shixi_daiyu'] = json_decode($res['shixi_daiyu'],true);
            $this->assign('user_data',$res);
            return $this->fetch('qz');
        }
    }


}